package VerifyInstalls;
import java.io.StringReader;

import game.BetPosition;
import game.CrapsTable;
import game.Dice;
import game.Player;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.rule.FactHandle;

/**
 * This is a sample class to launch a rule.
 */
public class DroolsVerifyOperation {
	private static Logger TestLogger = Logger.getLogger(DroolsVerifyOperation.class.getName());

    public static final void main(String[] args) {
    	TestLogger.setLevel(Level.DEBUG);
    	BasicConfigurator.configure();
        try {
            // load up the knowledge base
            KnowledgeBase kbase = readKnowledgeBase();
            StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
            KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
            // go !
            TestLogger.debug("Creating Table");
            CrapsTable testTable = new CrapsTable(1);
            TestLogger.debug("Creating P1");
            Player P1 = new Player(100);
            TestLogger.debug("Created new player: "+ P1.getID() + " with $" + P1.getBankroll());
            TestLogger.debug("Adding " + P1.getID() + " to the table");
            testTable.getMyManager().addPlayer(P1);
            TestLogger.debug(P1.getID() + " is placing a 10 passline bet");
            testTable.getMyManager().PlaceBet(P1,BetPosition.PassLine, 10);
            TestLogger.debug("Insert table into rules engine as a fact");
            FactHandle TableHandle = ksession.insert(testTable);
            Dice testDice = new Dice();
            testDice.setDie1(5);
            testDice.setDie2(5);
            TestLogger.debug("Insert dice: " + testDice.getDie1() + "," + testDice.getDie2() +" into rules engine as a fact");
            FactHandle DiceHandle = ksession.insert(testDice);
            TestLogger.debug("Firing Rules");
            ksession.fireAllRules();
            TestLogger.debug("Raising passline to 20 (this is actually illegal) and putting 40 odds");
            testTable.getMyManager().PlaceBet(P1, BetPosition.PassLine, 20);
            testTable.getMyManager().PlaceBet(P1, BetPosition.PassLineOdds, 40);
            TestLogger.debug("The point is " + testTable.isPointSet());
            TestLogger.debug(P1.getID() + " has $" + P1.getBankroll() + " in hand and active bets totalling $" + testTable.getMyManager().GetMoneyOnTable(P1));
            
            TestLogger.debug("Updating table fact to include recent changes from roll outcome");
            ksession.update(TableHandle, testTable);
            TestLogger.debug("Updating dice fact");
            testDice.setDie1(5);
            testDice.setDie2(2);
            ksession.update(DiceHandle, testDice);
            TestLogger.debug("Fireing rules again");
            ksession.fireAllRules();
            TestLogger.debug(P1.getID() + " has $" + P1.getBankroll() + " in hand and active bets totalling $" + testTable.getMyManager().GetMoneyOnTable(P1));
            testTable.close();
            logger.close();
            
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    private static KnowledgeBase readKnowledgeBase() throws Exception {
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newClassPathResource("WinLossRules.drl"), ResourceType.DRL);
        kbuilder.add(ResourceFactory.newClassPathResource("LegalBetRules.drl"),ResourceType.DRL);
        KnowledgeBuilderErrors errors = kbuilder.getErrors();
        if (errors.size() > 0) {
            for (KnowledgeBuilderError error: errors) {
                System.err.println(error);
            }
            throw new IllegalArgumentException("Could not parse knowledge.");
        }
        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
        return kbase;
    }


}
